package IdeaGui;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * User: Mike
 * Date: 10/29/11
 */

public class Writer {


    public Writer() {
    }

    void WriteData(boolean writeOut, MainGui guiData) throws IOException {        //Write out Files ================================================================
        if (writeOut) {
            File txtFile = new File("C:\\WhiskeyData\\output.txt");
            boolean fileExists = true;
            if (!txtFile.exists())
                fileExists = false;
            System.out.println("Thermocouple = " + guiData.getColumnC());
            String path = "C:\\WhiskeyData\\output.txt";
            DecimalFormat df1 = new DecimalFormat("###.##");
            FileWriter write = new FileWriter(path, true);
            PrintWriter text = new PrintWriter(write, true);
            if (!fileExists) {
                String header = "datetime"
                        + "\t" + "milliTime"
                        + "\t" + "kalman"
                        + "\t" + "columnC"
                        + "\t" + "columnF"
                        + "\t" + "ambientC"
                        + "\t" + "ambientF"
                        + "\t" + "bottleNumber"
                        + "\t" + "correctedAbv"
                        + "\t" + "vaporAbv"
                        + "\t" + "distillateTempC"
                        + "\t" + "boilerTempC"
//                        + "\t" + "flowRate"
                        + "\t" + "bottleFlowRateByRate"
                        + "\t" + "bottleFlowRateByJar"
                        + "\t" + "bottleFlowRatebyBottle"
                        + "\t" + "bottleVol"                    // 14
                        + "\t" + "HeatLevel"
                        + "\t" + "NeedleValveTurns"
                        + "\t" + "GoodABVmeasurement"
                        + "\t" + "vaporAbvCorr"
                        + "\t" + "Notes";
                text.println(header);
            }
            String output = CurrentTime()                       // 0
                    + "\t" + guiData.getSensorReadTime()        // 1
                    + "\t" + df1.format(guiData.getKalmanC())   // 2
                    + "\t" + guiData.getColumnC()               // 3
                    + "\t" + guiData.getColumnF()               // 4
                    + "\t" + guiData.getAmbientC()              // 5
                    + "\t" + guiData.getAmbientF()              // 6
                    + "\t" + guiData.getBottle()                // 7
                    + "\t" + guiData.getCorrAbv()               // 8
                    + "\t" + guiData.getVaporAbv()              // 9
                    + "\t" + guiData.getDistTemp()              // 10
                    + "\t" + guiData.getBoilerTemp()            // 11
                    + "\t" + guiData.getFlowRateByRate()        // 12
                    + "\t" + guiData.getFlowRateByJar()         // 14
                    + "\t" + guiData.getFlowRateByBottle()      // 15
                    + "\t" + guiData.getBottleVol()             // 16
                    + "\t" + guiData.getHeatLevel()             // 17
                    + "\t" + guiData.getNeedleValve()           // 18
                    + "\t" + guiData.getGoodAbvMeasurement()    // 19
                    + "\t" + guiData.getVaporAbvCorr()          // 20
                    + "\t" + guiData.getNotes()                 // 21
                    ;
            System.out.println(output);
//            assert text != null;
            text.println(output);
            text.flush();
            write.close();
            text.close();
        }
    }

    public static String CurrentTime() {
        Calendar calendar = new GregorianCalendar();

//        String am_pm;
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if (calendar.get(Calendar.AM_PM) == 0) {
//            am_pm = "AM";
        } else {
            hour = hour + 12;
//            am_pm = "PM";
        }
        month++;
//        Data.dateTimeS = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }

}
